package com.da.javatest.leetCode2;

public class Code543 {
    private int max;

    public int diameterOfBinaryTree(TreeNode root) {
        getMax(root);
        return max;
    }

    private int getMax(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int leftVal = getMax(node.left);
        int rightVal = getMax(node.right);
        if (max < leftVal + rightVal) {
            max = leftVal + rightVal;
        }
        return Math.max(leftVal, rightVal) + 1;
    }
}
